//初始化数据
$(function () {
    getData(1,6);
})
//更新数据
function getData(pageNo,length) {
    $.ajax({
        url: "/associator/list",
        type: "POST",
        dataType: "json",
        data:getSearch(pageNo,length),
        success:function (json) {
            var str="";
            var dataList = json.data.list;
            createPageNav({
                $container : $("#pageing"),
                pageCount :json.data.pages,
                currentNum : json.data.pageNum,
                afterFun : function(num){
                    getData(num,6);
                },
                hasCommonPage:false
            });

            for (var i = 0; i < dataList.length; i++) {
                str += "<tr>"
                    + '<th>' + dataList[i].associatorId + '</th>'
                    + "<th>" + dataList[i].phone + "</th>"
                    + "<th>" + dataList[i].associatorName + "</th>"
                    + "<th>" +validate( dataList[i].realName) + "</th>"
                    + "<th>" + formatDate(dataList[i].createTime)+ "</th>"
                    + "<th >"
                    +"<input type='button' value='初始化密码'  onclick='updataData("+dataList[i].associatorId+")'  class='btn btn-success btn-sm'/>"
                    +"<input type='button' value='删除'  onclick='deleteById("+dataList[i].associatorId+")'  class='btn btn-danger btn-sm'/>"
                    +"</th>"
                    + "</tr>"
            }
            $('#list').html(str);
        },
        error:function () {
            alert("操作失败");
        }
    })
}
//删除数据
function deleteById(id) {
    $.ajax({
        url: "/associator/delete",
        type: "POST",
        dataType: "json",
        data:{associatorId:id},
        success:function (json) {
            alert("成功");
            getData(1,6);
        },
        error:function () {
            alert("操作失败");
        }
    })
}
//提交修改内容
function updataData(id) {
    $.ajax({
        url:"/associator/update",
        type:"POST",
        dataType:"json",
        data:{associatorId:id},
        success:function (json) {
            alert("提交成功");
            getData(1,6);
        },error:function () {
            alert("提交失败")
        }
    })
}

//获取检索条件
function getSearch(pageNo,length) {
    var phone = $('[name="phone"]').val();
    var status = $('[name="status"]').val();
    var starttime = $('[name="starttime"]').val();
    var endtime = $('[name="endtime"]').val();
    var parm={pageNo:pageNo,length:length,phone:phone,status:status,starttime:starttime,endtime:endtime};
    return parm;
}
//时间戳转系统时间格式
function formatDate(now) {
    if (now==""){return "";}
    return new Date(now).toLocaleString();
}
var validate = function (data) {
    if (data == null) {
        return "";
    }
    return data;
}